#include "Renderer.h"

Renderer::Renderer(HWND hWnd){
	m_hWnd = hWnd;
	p_D3D = NULL;
	m_pkDevice = NULL;
}

bool Renderer::InitDX(HWND hWnd){
	HRESULT hr;
	m_hWnd = hWnd;
	p_D3D = Direct3DCreate9(D3D_SDK_VERSION);

	D3DDISPLAYMODE displayMode;
	hr = p_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode);

	if(hr != D3D_OK)
		return false;

	D3DPRESENT_PARAMETERS presentParameters;
	ZeroMemory(&presentParameters, sizeof(presentParameters));
	presentParameters.BackBufferCount = 1;
	presentParameters.BackBufferFormat = displayMode.Format;
	presentParameters.Windowed = TRUE;
	presentParameters.BackBufferHeight = 600;
	presentParameters.BackBufferWidth = 800;
	presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;

	hr = p_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, m_hWnd,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &m_pkDevice);

	if(hr != D3D_OK)
		return false;

	m_pkDevice->SetRenderState(D3DRS_LIGHTING, 0);
	m_pkDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

	D3DVIEWPORT9 kViewport;
	hr = m_pkDevice->GetViewport(&kViewport);
	if(hr != D3D_OK)
		return false;
	return true;
}

void Renderer::StartFrame(){
	m_pkDevice->Clear(0,NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(10,110,205), 1.0f, 0);
	m_pkDevice->SetFVF(CUSTOMFVF);
	m_pkDevice->BeginScene();
}

void Renderer::EndFrame(){
	m_pkDevice->EndScene();
	m_pkDevice->Present(NULL, NULL, NULL, NULL);
}
void Renderer::Draw(){
	m_pkDevice->SetFVF(CUSTOMFVF);
}

Renderer::~Renderer(void){
	if(m_pkDevice!=NULL)
		m_pkDevice->Release();

	if(p_D3D!=NULL)
		p_D3D->Release();
}